<html>
  <head>
    <title>Creating Custom Validation Rules</title>
    <link rel="stylesheet" type="text/css" href="../../styles/presentation.css" />
    <link rel="stylesheet" type="text/css" href="ms-help://Hx/HxRuntime/HxLink.css" />
    <script type="text/javascript" src="../../scripts/EventUtilities.js"></script>
    <script type="text/javascript" src="../../scripts/SplitScreen.js"></script>
    <script type="text/javascript" src="../../scripts/Dropdown.js"></script>
    <script type="text/javascript" src="../../scripts/script_loc.js"></script>
    <script type="text/javascript" src="../../scripts/script_manifold.js"></script>
    <script type="text/javascript" src="../../scripts/script_feedBack.js"></script>
    <script type="text/javascript" src="../../scripts/CheckboxMenu.js"></script>
    <script type="text/javascript" src="../../scripts/CommonUtilities.js"></script>
  </head>
  <body>
    <input type="hidden" id="userDataCache" class="userDataStyle" />
    <input type="hidden" id="hiddenScrollOffset" />
    <img id="collapseImage" style="display:none; height:0; width:0;" src="../../icons/collapse_all.gif" title="Collapse image" />
    <img id="expandImage" style="display:none; height:0; width:0;" src="../../icons/expand_all.gif" title="Expand Image" />
    <img id="collapseAllImage" style="display:none; height:0; width:0;" src="../../icons/collapse_all.gif" />
    <img id="expandAllImage" style="display:none; height:0; width:0;" src="../../icons/expand_all.gif" />
    <img id="dropDownImage" style="display:none; height:0; width:0;" src="../../icon/dropdown.gif" />
    <img id="dropDownHoverImage" style="display:none; height:0; width:0;" src="../../icons/dropdownHover.gif" />
    <img id="copyImage" style="display:none; height:0; width:0;" src="../../icons/copycode.gif" title="Copy image" />
    <img id="copyHoverImage" style="display:none; height:0; width:0;" src="../../icons/copycodeHighlight.gif" title="CopyHover image" />
    <div id="header">
      <table id="topTable" cellspacing="0" cellpadding="0">
        <tr>
          <td>
            <span onclick="ExpandCollapseAll(toggleAllImage)" style="cursor:default;" onkeypress="ExpandCollapseAll_CheckKey(toggleAllImage, event)" tabindex="0">
              <img ID="toggleAllImage" class="toggleAll" src="../../icons/collapse_all.gif" />
              <label id="collapseAllLabel" for="toggleAllImage" style="display: none;">Collapse All</label>
              <label id="expandAllLabel" for="toggleAllImage" style="display: none;">Expand All</label>
            </span>
          </td>
        </tr>
      </table>
      <table id="bottomTable" cellpadding="0" cellspacing="0">
        <tr id="headerTableRow1">
          <td align="left">
            <span id="runningHeaderText">DevExpress Contrib</span>
          </td>
        </tr>
        <tr id="headerTableRow2">
          <td align="left">
            <span id="nsrTitle">
                  Creating Custom Validation Rules
               </span>
          </td>
          <td align="center" style="width: 1px; padding: 0px">
            <img src="../../images/DevExContrib 32.png" alt="DevExpress Contrib" />
          </td>
        </tr>
        <tr id="headerTableRow3">
          <td align="left">
            <span id="headfb" class="feedbackhead" />
          </td>
        </tr>
      </table>
      <table id="gradientTable">
        <tr>
          <td class="nsrBottom" background="../../icons/gradient.gif" />
        </tr>
      </table>
    </div>
    <div id="mainSection">
      <div id="mainBody">
        <div class="Content">
          <p>In it's simplest form, a custom validator overrides the Validate() method to validate a column value.</p>
          <pre lang="cs" numberLines="true" outlining="true" title="Simple custom validation rule">
   public class NotBlankTrimmedColumnValidationRule : ColumnValidationRule
   {
      public NotBlankTrimmedColumnValidationRule() : base() { }

      public override bool Validate(object value)
      {
          if (value != null && value is string)
              value = (value as string).Trim();
          bool result = ValidationHelper.Validate(value, ConditionOperator.IsNotBlank, value, null, null);
          if (!result)
              ErrorText = "This value cannot be blank or contain only whitespace.";
          return result;
      }
   }</pre>
          <p>The value passed in to Validate() is the row cell value of the column to be validated. In this case
          we are using the Developer Express ValidationHelper to finish our validation. We could have easily
          coded this part as well.</p>
          <p>Within the validator you can get additional information about the row or view including the RowHandle,
          FocusedColumn, ColumnByFieldName, GetRowCellValue, etc.</p>
          <p>The following example gets the value of the 'Age' column and ensures that the 'Code' column
          ends with a space and then the age.</p>
          <pre lang="cs" numberLines="true" outlining="true" title="A more complex custom validation rule used as a RowValidator">
   public class ValidateRow : ColumnValidationRule
   {
      public ValidateRow() : base() { }

      public override bool Validate(object value)
      {
         ErrorText = String.Empty;
         object codeObj = GetRowCellValue("Code");
         string code = codeObj.ToString();
         object ageObj = GetRowCellValue("Age");
         int age = Convert.ToInt32(ageObj);
         bool result = code.EndsWith(" " + age.ToString());
         if (!result)
         {
            ErrorText = "'Code' must end with 'Age'";
            SetColumnError(ColumnByFieldName("Code"), ErrorText);
         }
         return result;
      }
   }</pre>
          <p>&nbsp;</p>
          <p>
            <a href="Task-Based Help.html">Back to task-based help.</a>
          </p>
        </div>
      </div>
      <div id="footer">
        <div class="footerLine">
          <img width="100%" height="3px" src="../../icons/footer.gif" title="Footer image" />
        </div>
        <p />
        <div id="DevLynxVersion">
           <p><small>v2009.0.3332.39062 (2009-02-14  21:42:04)</small></p>
        </div>
        <div id="DevLynxCopyright">
           <p>Copyright &copy; 2008-2009, DevLynx</p>
        </div>
      </div>
    </div>
  </body>
</html>
<!-- @SortOrder 6 -->
